#!/bin/bash
set -x
echo "------- Please place this script in the IEdgeinsights peer directory -------"
env_file="IEdgeInsights/build/.env"
sed -i 's/DEV_MODE=.*/DEV_MODE=true/g' $env_file
eii_version=$(cat $env_file | grep -i EII_VERSION | awk -F '=' '{print $2}')

pre_requisitesfile="IEdgeInsights/build/pre_requisites.sh"
sed -i 's!google.com!baidu.com!g' $pre_requisitesfile

common_dockerfile="IEdgeInsights/common/dockerfiles/Dockerfile.eiibase"
sed -i 's!git clone https://github.com/xeipuuv!git clone https://gitee.com/mirrors_xeipuuv!g' $common_dockerfile
sed -i 's!git clone https://github.com/golang/glog!git clone https://gitee.com/mirrors_openshift/golang-glog!g' $common_dockerfile

common_util_istlfile="IEdgeInsights/common/util/c/install.sh"
sed -i 's!https://github.com/netmail-open/wjelement!https://gitee.com/xiaowei315/wjelement!g' $common_util_istlfile

provision_dockerfile="IEdgeInsights/build/provision/dep/Dockerfile"
lib_installfile="IEdgeInsights/common/eii_libs_installer.sh"
sed -i 's!git clone https://github.com/etcd-io!git clone https://gitee.com/mirrors!g' $lib_installfile
sed -i 's!git clone https://github.com/golang/glog!git clone https://gitee.com/mirrors_openshift/golang-glog!g' $lib_installfile
webvis_dockerfile="IEdgeInsights/WebVisualizer/Dockerfile"
sed -i 's!code.jquery.com!s3.pstatp.com/cdn/expire-1-M/jquery/3.4.1!g' $webvis_dockerfile
sed -i 's/jquery-3.4.1.min.js/jquery.min.js/g' $webvis_dockerfile

# add go proxy
EtcdUI_dockerfile="IEdgeInsights/EtcdUI/Dockerfile"
spec_line=`cat $EtcdUI_dockerfile | grep -n 'go build' | awk -F ":" '{print $1}'`
add_line=$(( $spec_line-1 ))
sed -i "$add_line i ENV GOPROXY https://goproxy.io" $EtcdUI_dockerfile

# add pip source
vi_dockerfile="IEdgeInsights/VideoIngestion/Dockerfile"
sed -i 's!install -r vi_requirements.txt!install -r vi_requirements.txt -i https://mirrors.aliyun.com/pypi/simple!g' $vi_dockerfile
va_dockerfile="IEdgeInsights/VideoAnalytics/Dockerfile"
sed -i 's!install -r va_requirements.txt!install -r va_requirements.txt -i https://mirrors.aliyun.com/pypi/simple!g' $va_dockerfile
grep -nr "install -r requirements.txt" | grep -v README | awk -F ":" '{print$1}' | xargs sed -i 's!install -r requirements.txt!install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple!g'

influx_dockerfile="IEdgeInsights/InfluxDBConnector/Dockerfile"
sed -i 's!https://github.com/influxdata/influxdb!https://gitee.com/mirrors/InfluxDB!g' $influx_dockerfile
kapaci_dockerfile="IEdgeInsights/Kapacitor/Dockerfile"
sed -i 's!https://github.com/influxdata!https://gitee.com/mirrors!g' $kapaci_dockerfile
sed -i 's!https://github.com/golang/glog!https://gitee.com/mirrors_openshift/golang-glog!g' $kapaci_dockerfile
telegraf_dockerfile="IEdgeInsights/Telegraf/Dockerfile"
sed -i 's!golang.org/dl!golang.google.cn/dl!g' $telegraf_dockerfile
sed -i 's!https://github.com/influxdata/telegraf!https://gitee.com/mirrors_influxdata/telegraf!g' $telegraf_dockerfile
grpc_line=`cat $telegraf_dockerfile | grep -n 'google.golang.org/grpc' | awk -F ":" '{print $1}'`
goproxyadd_line=$(( $grpc_line-1 ))
sed -i "$goproxyadd_line i ENV GOPROXY https://goproxy.io" $telegraf_dockerfile
imagestore_dockerfile="IEdgeInsights/ImageStore/Dockerfile"
sed -i 's!https://github.com/mitchellh/go-homedir!https://gitee.com/mirrors_addons/go-homedir!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/crypto!https://gitee.com/mirrors/crypto!g' $imagestore_dockerfile
sed -i 's!https://github.com/minio/minio-go!https://gitee.com/mirrors_minio/minio-go!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/net!https://gitee.com/virnop/golang-net!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/text!https://gitee.com/virnop/golang-text!g' $imagestore_dockerfile
sed -i 's!https://github.com/golang/sys!https://gitee.com/virnop/golang-sys!g' $imagestore_dockerfile
sed -i 's!https://github.com/go-ini/ini!https://gitee.com/mirrors/ini.git!g' $imagestore_dockerfile

azure_dockerfile="IEdgeInsights/AzureBridge/modules/AzureBridge/Dockerfile"
sed -i 's!https://github.com/kragniz/python-etcd3!https://gitee.com/mirrors/python-etcd3!g' $azure_dockerfile
safestring_file="IEdgeInsights/common/util/c/IntelSafeString/cmake/IntelSafeString.txt.in"
sed -i 's!https://github.com/intel/safestringlib!https://gitee.com/mirrors_intel/safestringlib!g' $safestring_file
find IEdgeInsights/OpcuaExport/OpcuaBusAbstraction -type f -name "Makefile" | xargs sed -i 's!https://github.com/intel/safestringlib!https://gitee.com/mirrors_intel/safestringlib!g'

grep -nr "curl -L --retry 5 https://github.com/.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!curl -L https://github.com!curl -L --retry 5 https://github.com!'

find IEdgeInsights/common -type f -name "install.sh" | xargs sed -i 's!wget!wget --tries 5!g'

grep -nr "https://github.com/coreos.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/coreos/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-amd64.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786635/download/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz!g'

grep -nr "https://github.com/DaveGamble.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/DaveGamble/cJSON/archive/v${cjson_version}.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786633/download/cJSON-${cjson_version}.tar.gz!g'

if [ "$eii_version" == "2.5.2" ];then
    grep -nr "https://github.com/zeromq.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/zeromq/libzmq/releases/download/v4.3.3/zeromq-${zeromq_version}.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/892628/download/zeromq-${zeromq_version}.tar.gz!g'
else
    grep -nr "https://github.com/zeromq.*.gz" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/zeromq/libzmq/releases/download/v4.3.1/zeromq-${zeromq_version}.tar.gz!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786634/download/zeromq-${zeromq_version}.tar.gz!g'
fi
grep -nr "https://github.com/twbs/.*.zip" | awk -F ":" '{print$1}' | xargs sed -i 's!https://github.com/twbs/bootstrap/releases/download/v4.0.0/bootstrap-4.0.0-dist.zip!https://gitee.com/xiaowei315/eii_3rd_pkg/attach_files/786714/download/bootstrap-4.0.0-dist.zip!g'

